<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>大健康助手 - 专业医疗咨询</title>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
    <style>
        :root {
            --primary: #1a73e8;
            --secondary: #34a853;
            --accent: #4285f4;
            --light: #f8f9fa;
            --dark: #202124;
            --success: #34a853;
            --warning: #fbbc05;
            --danger: #ea4335;
            --radius: 12px;
            --shadow: 0 4px 12px rgba(0,0,0,0.08);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Noto Sans SC', sans-serif;
            background: linear-gradient(135deg, #f0f7ff 0%, #e6f4ff 100%);
            color: var(--dark);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
            padding: 20px 0;
        }

        .title {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 15px;
            color: var(--primary);
            font-size: 2.5rem;
            font-weight: 700;
            margin-bottom: 10px;
        }

        .subtitle {
            color: var(--dark);
            font-size: 1.1rem;
            font-weight: 400;
            opacity: 0.8;
        }

        .card {
            background: white;
            border-radius: var(--radius);
            box-shadow: var(--shadow);
            padding: 30px;
            margin-bottom: 30px;
            transition: all 0.3s ease;
        }

        .input-group {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }

        #question-input {
            flex: 1;
            padding: 16px 20px;
            border: 2px solid #e0e0e0;
            border-radius: var(--radius);
            font-size: 1rem;
            transition: border-color 0.3s;
        }

        #question-input:focus {
            outline: none;
            border-color: var(--accent);
        }

        #submit-btn {
            background: var(--primary);
            color: white;
            border: none;
            border-radius: var(--radius);
            padding: 0 30px;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        #submit-btn:hover {
            background: #0d62c9;
            transform: translateY(-2px);
        }

        #submit-btn:disabled {
            background: #cccccc;
            cursor: not-allowed;
            transform: none;
        }

        .history-section {
            margin-top: 30px;
        }

        .history-title {
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 1.3rem;
            margin-bottom: 20px;
            color: var(--dark);
        }

        .qa-card {
            background: var(--light);
            border-radius: var(--radius);
            padding: 20px;
            margin-bottom: 20px;
            border-left: 4px solid var(--accent);
        }

        .question {
            font-weight: 500;
            margin-bottom: 15px;
            color: var(--dark);
            display: flex;
            gap: 10px;
        }

        .answer {
            padding: 15px;
            background: white;
            border-radius: 8px;
            line-height: 1.6;
        }

        .timestamp {
            text-align: right;
            font-size: 0.85rem;
            color: #5f6368;
            margin-top: 10px;
        }

        .loader {
            display: none;
            text-align: center;
            padding: 20px;
        }

        .dot-flashing {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color: var(--primary);
            margin: 0 4px;
            animation: dot-flashing 1s infinite alternate;
        }

        .dot-flashing:nth-child(2) {
            animation-delay: 0.2s;
        }

        .dot-flashing:nth-child(3) {
            animation-delay: 0.4s;
        }

        @keyframes dot-flashing {
            0% { opacity: 0.3; transform: scale(0.8); }
            100% { opacity: 1; transform: scale(1.2); }
        }

        .error-message {
            color: var(--danger);
            padding: 10px;
            background: rgba(234, 67, 53, 0.1);
            border-radius: 8px;
            margin-top: 15px;
            display: none;
        }

        @media (max-width: 600px) {
            .title {
                font-size: 2rem;
            }

            .input-group {
                flex-direction: column;
            }

            #submit-btn {
                padding: 16px;
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <div class="title">
                <i data-feather="heart"></i>
                <h1>大健康助手</h1>
            </div>
            <p class="subtitle">专业医疗咨询平台 · 您的健康守护者</p>
        </div>

        <div class="card">
            <h2>健康问题咨询</h2>
            <p style="margin: 10px 0 20px; color: #5f6368;">请输入您的医疗问题，我们将为您提供专业解答</p>

            <div class="input-group">
                <input type="text" id="question-input" placeholder="例如：糖尿病患者的饮食注意事项有哪些？">
                <button id="submit-btn">
                    <i data-feather="send"></i> 提交咨询
                </button>
            </div>

            <div class="loader" id="loader">
                <div class="dot-flashing"></div>
                <div class="dot-flashing"></div>
                <div class="dot-flashing"></div>
            </div>

            <div class="error-message" id="error-message"></div>
        </div>

        <div class="card">
            <div class="history-title">
                <i data-feather="clock"></i>
                <h3>咨询记录</h3>
            </div>
            <div id="history-container">
                <!-- 历史问答记录将动态添加到这里 -->
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            feather.replace();

            const questionInput = document.getElementById('question-input');
            const submitBtn = document.getElementById('submit-btn');
            const loader = document.getElementById('loader');
            const errorMsg = document.getElementById('error-message');
            const historyContainer = document.getElementById('history-container');

            // 提交问题函数
            async function submitQuestion() {
                const question = questionInput.value.trim();

                if (!question) {
                    showError('请输入有效的医疗问题');
                    return;
                }

                // 显示加载状态
                loader.style.display = 'block';
                submitBtn.disabled = true;
                errorMsg.style.display = 'none';

                try {
                    // 发送请求到后端API
                    const response = await fetch('/api/predict', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ question: question })
                    });

                    const data = await response.json();

                    if (!response.ok) {
                        throw new Error(data.error || '请求失败');
                    }

                    // 添加到历史记录
                    addToHistory(data);

                    // 清空输入框
                    questionInput.value = '';

                } catch (err) {
                    showError(err.message);
                } finally {
                    loader.style.display = 'none';
                    submitBtn.disabled = false;
                }
            }

            // 添加到历史记录
            function addToHistory(data) {
                const historyItem = document.createElement('div');
                historyItem.className = 'qa-card';
                historyItem.innerHTML = `
                    <div class="question">
                        <i data-feather="user"></i>
                        <strong>问：</strong>${data.question}
                    </div>
                    <div class="answer">
                        <div><i data-feather="activity"></i> <strong>答：</strong>${data.answer}</div>
                        <div class="timestamp">${data.timestamp}</div>
                    </div>
                `;

                // 将新记录添加到顶部
                historyContainer.insertBefore(historyItem, historyContainer.firstChild);

                // 更新图标
                feather.replace();
            }

            // 显示错误信息
            function showError(message) {
                errorMsg.textContent = message;
                errorMsg.style.display = 'block';
            }

            // 事件监听
            submitBtn.addEventListener('click', submitQuestion);

            questionInput.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    submitQuestion();
                }
            });
        });
    </script>
</body>
</html>